# Utility for ProjectEuler

import unittest
import math



def isPrime(N): 
    N_root = int(math.sqrt(N))
    found_factor = False
    for i in range(2, N_root + 1):
        if N % i == 0:
            found_factor = True
            break
    return (not found_factor)

 
 
def getAllPrimes(N):
    result = []
    for i in range(2, N + 1):
        if isPrime(i):
            result.append(i)
    return result    

    
    
def getAllPrimeFactors(N):
    result = {}
    factor = 2
    while factor <= N:
        num = 0
        while N % factor == 0:
            N /= factor
            num += 1
        if num > 0:
            result[factor] = num
        factor += 1
    return result    

    
    
class TestUtility(unittest.TestCase):
    def test_isPrime(self):
        self.assertTrue(isPrime(2))
        self.assertTrue(isPrime(3))    
        self.assertTrue(isPrime(11))
        self.assertFalse(isPrime(12))
        self.assertFalse(isPrime(4294967297))
        self.assertTrue(isPrime(6857))  
    
    
    def test_findAllPrimes(self):
        self.assertEqual(getAllPrimes(2), [2])
        self.assertEqual(getAllPrimes(10), [2, 3, 5, 7])
    
    
    def testGetAllPrimeFactors(self):
        self.assertEqual({}, getAllPrimeFactors(1))
        self.assertEqual({2:1}, getAllPrimeFactors(2)) 
        self.assertEqual({3:1}, getAllPrimeFactors(3))
        self.assertEqual({2:2}, getAllPrimeFactors(4))
        self.assertEqual({2:1, 3:1}, getAllPrimeFactors(6))
        self.assertEqual({2:3}, getAllPrimeFactors(8))
        self.assertEqual({2:2, 3:1}, getAllPrimeFactors(12))

        
if __name__ == '__main__':
    TestUtility = unittest.TestLoader().loadTestsFromTestCase(TestUtility)
    unittest.TextTestRunner(verbosity=2).run(TestUtility)
 




    